package com.dingtalk.yuanhua_spring_project.repository;

import com.dingtalk.yuanhua_spring_project.dto.InstanceInfoDTO;
import com.dingtalk.yuanhua_spring_project.pojo.Instance;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface InstanceRepository extends JpaRepository<Instance, Long> {
    // 自定义查询方法：根据 instanceId 和 modifiedTime 判断是否存在
    @Query("SELECT CASE WHEN COUNT(i) > 0 THEN true ELSE false END FROM Instance i WHERE i.instanceId = :instanceId AND i.modifiedTime = :modifiedTime")
    boolean existsByInstanceIdAndModifiedTime(@Param("instanceId") String instanceId, @Param("modifiedTime") String modifiedTime);

    @Query("SELECT new com.dingtalk.yuanhua_spring_project.dto.InstanceInfoDTO(i.instanceId, i.jijinName, i.jianchengName, i.projectName) FROM Instance i ")
    List<InstanceInfoDTO> findAllInstanceInfo();

    @Query("SELECT i.instanceId FROM Instance i WHERE i.isInvested = true ")
    List<String> findAllInstanceIds();

    // 自定义查询方法：根据 folderList 和 modifiedTime 判断是否存在
    @Query("SELECT CASE WHEN COUNT(i) > 0 THEN true ELSE false END FROM Instance i WHERE i.folderList = :folderList AND i.modifiedTime = :modifiedTime")
    boolean existsByFolderListAndModifiedTime(@Param("folderList") String folderList, @Param("modifiedTime") String modifiedTime);
}
